package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOPeratiion{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书!");
        //查找要删除的书
        int currentSize = bookList.getUsedSize();
        if(currentSize == 0){
            System.out.println("书架已空 不能删除！");
            return;
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的书名：");
        String name = scanner.nextLine();

        int index = -1;
        for (int i = 0; i < currentSize; i++) {
            Book book1 = bookList.getBook(i);
            if(book1.getName().equals(name)){
                index = i;
                break;
            }
        }
        //如果index ！= -1 说明有这本书  开始删除
        if(index == -1) {
            System.out.println("没有要删除的书！");
            return;
        }
        for (int i = index; i < currentSize-1; i++) {
            Book book1 = bookList.getBook(i+1);
            bookList.setBooks(i,book1);
        }
        //当书删除之后 usedSize要--
        bookList.setUsedSize(currentSize-1);
    }
}
